package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.Vo.GoodsVo;
import cn.leyaber.hrm.clients.UserFeignClient;
import cn.leyaber.hrm.domain.Address;
import cn.leyaber.hrm.domain.Goods;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.dto.entity.SfExamineMerchantsInformation;
import cn.leyaber.hrm.dto.entity.SfGoodsDetails;
import cn.leyaber.hrm.fegin.GoodsBasicService;
import cn.leyaber.hrm.fegin.MerchantsBasicService;
import cn.leyaber.hrm.query.GoodsQuery;
import cn.leyaber.hrm.service.IGoodsService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private GoodsBasicService sfGoodsDetailsBasicService;
    @Autowired
    private MerchantsBasicService examineMerchantsInformationBasicService;
    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 修改状态
     * @param goods
     * @return
     */
    @PostMapping("/Modify_status")
    public AjaxResult Modify_status(@RequestBody Goods goods){
        try {
            goodsService.Modify_status(goods);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改对象失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            goodsService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
    /**
    * 保存和修改公用的
    * @param goods  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Goods goods){
        try {
            if(goods.getId()!=null){
                goodsService.updateById(goods);
            }else{
                goodsService.insert(goods);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            goodsService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Goods get(@PathVariable("id")Long id)
    {
        return goodsService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Goods> list(){

        return goodsService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<GoodsVo> json(@RequestBody GoodsQuery query)
    {
        Page<Goods> page = new Page<Goods>(query.getPage(),query.getRows());
        page = goodsService.selectPage(page);
        Page<GoodsVo> page1 = JSON.parseObject(JSON.toJSONString(page), Page.class);
        ArrayList<GoodsVo> objects = new ArrayList<>();
        for (Goods record : page.getRecords()) {
            GoodsVo goodsVo = JSON.parseObject(JSON.toJSONString(record), GoodsVo.class);
            if (StringUtils.isEmpty(record.getUserWholenameId())&&StringUtils.isEmpty(record.getSfExamineMerchantsInformationId())
               &&StringUtils.isEmpty(record.getSfGoodsDetailsId())&&StringUtils.isEmpty(record.getUserAddressId())
               &&StringUtils.isEmpty(record.getSfGoodsDetailsId())&&StringUtils.isEmpty(record.getSfExamineMerchantsInformationId())){
                objects.add(goodsVo);
                continue;
            }

            SfGoodsDetails byId = sfGoodsDetailsBasicService.getById(record.getSfGoodsDetailsId());
            goodsVo.setGoodsName(byId.getGoodsName());
            SfExamineMerchantsInformation byId1 = examineMerchantsInformationBasicService.getById(record.getSfExamineMerchantsInformationId());
            goodsVo.setMerchantsName(byId1.getMerchantsName());
            Wholename wholename = userFeignClient.selectById(record.getUserWholenameId());
            Address address = userFeignClient.get(record.getUserAddressId());
            goodsVo.setDetailedAddress(address.getDetailedAddress());
            goodsVo.setUser(wholename.getUser());
            objects.add(goodsVo);
        }
        page1.setRecords(objects);
        return new PageList<GoodsVo>(page1.getTotal(),page1.getRecords());
    }
}
